package com.android.functionmanagement.receiver;

import com.android.functionmanagement.BaseApplication;
import com.android.functionmanagement.globle.ConstantGloble;
import com.android.functionmanagement.service.DaemonService;
import com.android.functionmanagement.util.DeviceUtils;
import com.android.functionmanagement.util.LoadUserInfo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
//import android.net.NetworkInfo.State;
import android.util.Log;

public class NetworkChangeReceiver extends BroadcastReceiver {
//	private static final String TAG = "NetworkChangeReceiver";
//	private State wifiState = null;
//	private State mobileState = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
//		wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//		mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		NetworkInfo info = cm.getActiveNetworkInfo();
		//if (((wifiState != null && State.CONNECTED == wifiState) || (mobileState != null && State.CONNECTED == mobileState))
		//		&& (info != null && info.isConnected() && info.isAvailable())) {//需要在没用网络的情况下才触发
		if (info != null && info.isConnected()) {
			Log.d("DHYCO", "网络连接成功！");
			final BaseApplication baseApplication = (BaseApplication) context
					.getApplicationContext();
			baseApplication.initJpush();
			LoadUserInfo.getUserInfo(context, false);
			notifyDaemon(context);
		}  else  {
			Log.d("DHYCO", "手机没有任何网络...");
		}

	}
	private void notifyDaemon(Context context){
		Intent it = new Intent(context, DaemonService.class);
		it.putExtra(ConstantGloble.EXTRATYPE,
				ConstantGloble.ALARM_ACTION_NETWORK_DATA);
		context.startService(it);
	}
	public boolean haveInternet(Context context){
		return DeviceUtils.haveInternet(context);
	}

}
